<?php

define("AJAX_CONST_OK", 1);
define("AJAX_CONST_ERROR", 2);
define("AJAX_CONST_REDIRECT", 3);

class Ajax
{
    var $flag = NULL;
    var $result = NULL;

    public function Ajax () {
    }

    public function Parse ($json) {
        $rlt = json_decode($json);
        if (!isset($rlt->flag)) {
            return false;
        }
        if (!isset($rlt->result)) {
            return false;
        }
        if ($rlt->flag == AJAX_CONST_OK) {
            $this->flag = $rlt->flag;
            $this->result = $rlt->result;
        }
        return true;
    }

    public function Send () {
        $rlt = array("flag"=>$this->flag,
            "result"=>$this->result);
        echo json_encode($rlt);
    }

    public static function Redirect ($url) {
        $ajax = new Ajax();
        $ajax->flag = AJAX_CONST_REDIRECT;
        $ajax->result = $url;
        $ajax->Send();
    }

    public static function Error ($msg) {
        $ajax = new Ajax();
        $ajax->flag = AJAX_CONST_ERROR;
        $ajax->result = $msg;
        $ajax->Send();
    }

    public static function Post ($mix) {
        $ajax = new Ajax();
        $ajax->flag = AJAX_CONST_OK;
        $ajax->result = $mix;
        $ajax->Send();
    }

}


